﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ContractorTimesheet.DataModel;
using ContractorTimesheet.Infrastructure;
using ContractorTimesheet.Models;
using ContractorTimesheet.Repository;
using PerpetuumSoft.Knockout;

namespace ContractorTimesheet.Controllers
{
    public class HolidaysController : BaseController
    {
        private HolidayRepository repository = new HolidayRepository();

        public ActionResult Index()
        {
            var model = new HolidayEditorModel();

            model.Holidays.AddRange(repository.GetAllHolidays());

            return View(model.SortByDateDescending());
        }

        public ActionResult AddHoliday(HolidayEditorModel model)
        {
            Holiday newHoliday = new Holiday(model.NewYear, model.NewMonth, model.NewDay, model.NewDescription);

            repository.Add(newHoliday);

            model.Holidays.Add(newHoliday);

            model.ResetNewHoliday();

            return Json(model.SortByDateDescending());
        }

        public ActionResult DeleteHoliday(HolidayEditorModel model, int index)
        {
            Holiday holiday = model.Holidays[index];

            repository.DeleteAsync(holiday.Id);

            model.Holidays.Remove(holiday);

            return Json(model);
        }
    }
}